package com.ficus.road.maintain.third.zh.density;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.density.UnitDensity;
import com.ficus.road.maintain.core.model.density.UnitDensityZH;
import com.ficus.road.maintain.dao.density.UnitDensityDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.ficus.road.maintain.third.zh.coverage.ZHCycleNum;
import com.ficus.road.maintain.util.MyDateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/3/16 下午7:34
 */
@Component
public class ZHUnitDensity extends BaseZH {

    @Autowired
    private UnitDensityDao unitDensityDao;
    @Autowired
    private ZHCycleNum zhCycleNum;

    @PostConstruct
    void init() {
        arg0 = "ytkjGetSectionBhRate";
        errMsg = "调用中海接口获取标段病害密度异常！";
        successMsg = "调用中海接口获取标段病害密度成功！";
    }

    //    @Scheduled(cron = "*/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0/10 6-18 * * ?", zone = "GMT+8:00")
    public void refreshUnitCoverageWeek() {
        String todayStr = MyDateUtil.dateFormatStr(new Date(), "yyyy-MM-dd");
        arg1 = "{'startDate':'" + todayStr + "','endDate':'" + todayStr + "'}";
        String densityStr = getZHData();
        if (ObjectUtils.isEmpty(densityStr)) {
            return;
        }
        String cycleNum = zhCycleNum.getCycleNum(todayStr);
        List<UnitDensityZH> unitDensityZHList = JSONArray.parseArray(densityStr, UnitDensityZH.class);
        List<UnitDensity> unitDensityList = unitDensityZHList.stream().map(item -> item.convert2UnitDensity(item, todayStr, cycleNum)).collect(Collectors.toList());
        unitDensityList.forEach(unitDensity -> {
            unitDensityDao.saveOrUpdate(unitDensity);
        });
    }
}
